package com.situ.skymall.user.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.model.UserAddress;
import com.situ.service.UserAddressService;
import com.situ.skymall.user.api.mapper.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户地址服务实现类
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    private UserAddressMapper userAddressMapper;
    @Autowired
    public void setUserAddressService(UserAddressMapper userAddressMapper) {
        this.userAddressMapper = userAddressMapper;
    }

    @Override
    public List<UserAddress> findByUserId(Integer userId) {
        List<UserAddress> addresses = userAddressMapper.selectByUserId(userId);
        return addresses;
    }

    @Override
    public UserAddress findDefaultAddress(Integer userId) {
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUserId, userId)
               .eq(UserAddress::getIsDefault, 1);
        return this.getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean setDefaultAddress(Integer id, Integer userId) {
        // 先将该用户的所有地址设置为非默认
        LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserAddress::getUserId, userId)
                    .set(UserAddress::getIsDefault, 0);
        this.update(updateWrapper);

        // 将指定地址设置为默认
        UserAddress userAddress = new UserAddress();
        userAddress.setId(id);
        userAddress.setIsDefault(1);
        return this.updateById(userAddress);
    }

    @Override
    public boolean addAddress(UserAddress userAddress) {
        // 如果是默认地址，需要将其他地址设置为非默认
        if (userAddress.getIsDefault() == 1) {
            LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserAddress::getUserId, userAddress.getUserId())
                        .set(UserAddress::getIsDefault, 0);
            this.update(updateWrapper);
        }
        //设置默认值
        userAddress.setCreatedAt(LocalDateTime.now());
        userAddress.setUpdatedAt(LocalDateTime.now());

        return this.save(userAddress);
    }

    @Override
    @Transactional
    public boolean updateAddress(UserAddress userAddress) {
        // 如果是默认地址，需要将其他地址设置为非默认
        if (userAddress.getIsDefault() == 1) {
            LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserAddress::getUserId, userAddress.getUserId())
                        .ne(UserAddress::getId, userAddress.getId())
                        .set(UserAddress::getIsDefault, 0);
            this.update(updateWrapper);
        }
        return this.updateById(userAddress);
    }

    @Override
    public boolean deleteAddress(Integer id) {
        return this.removeById(id);
    }
}